package com.totoro.tyrank.manager;

import com.totoro.tyrank.TyRank;
import com.totoro.tyrank.data.PlayerData;
import com.totoro.tyrank.data.RankingData;
import com.totoro.tyrank.utils.ListUtil;
import com.totoro.tyrank.utils.RankingUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.serverct.ersha.api.AttributeAPI;
import org.serverct.ersha.attribute.data.AttributeData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AttributeManager {

    private final TyRank plugin;

    public AttributeManager(TyRank plugin) {
        this.plugin = plugin;
    }


    /**
     * 加载所有玩家属性
     */
    public void loadOnlinePlayerAttribute() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            loadAttribute(player);
        }
    }

    /**
     * @author totoro
     * @description 加载单个玩家的属性，根据所有排行榜名次
     * @date  10:24
     * @param [player]
     * @return void
     **/
    public void loadAttribute(Player player) {
        // 所有属性的List集合
        List<String> allAttr = new ArrayList<>();
        // 获取该玩家所有排行榜的数据
        Map<String, RankingData> currentRankDataMap = TyRank.getRankingManager().getRankDataMap();
        currentRankDataMap.forEach((rankId, rankingData) -> {
            // 该排行榜有无该玩家数据
            PlayerData playerData = TyRank.getPlayerDataManager().getPlayerDataByRankId(player, rankId);
            if(null == playerData) {
                return;
            }
            // 该玩家有无名次
            if(null == playerData.getRanking() || -1 == playerData.getRanking()) {
                return;
            }
            // 该排行是否在本次排行榜中找到领取预览
            RankingData.RankingReceive receive = TyRank.getRankingReceiveManager().getReceive(rankId, playerData.getRanking());
            if(null == receive) {
                return;
            }
            RankingData.RankingReceive.Attribute attributeEffect = receive.getAttribute();
            if(null == attributeEffect) {
                return;
            }
            List<String> giveAttribute = attributeEffect.getGiveAttribute();
            if(ListUtil.isBlank(giveAttribute)) {
                return;
            }
            allAttr.addAll(giveAttribute);
        });
        if(ListUtil.isNotBlank(allAttr)) {
            // 给予属性
            loadAttribute(player, allAttr);
        }
        updateAttribute(player);
    }

    /**
     * @author totoro
     * @description 加载单个玩家的属性，自定义属性List
     * @date  10:44
     * @param [p, attr]
     * @return void
     **/
    public void loadAttribute(Player p, List<String> attr) {
        if(TyRank.getConfigManager().getConfigData().getUseAttribute().equals("ap")) {
            // 先获取该玩家所有属性数据
            AttributeData attributeData = AttributeAPI.getAttrData(p);
            // 删除一次属性源
            AttributeAPI.takeSourceAttribute(attributeData, "TyRank");
            // 添加属性源
            AttributeAPI.addSourceAttribute(attributeData, "TyRank", attr);
        }
        updateAttribute(p);
    }

    public void updateAttribute(Player player) {
        if(TyRank.getConfigManager().getConfigData().getUseAttribute().equals("ap")) {
            AttributeAPI.updateAttribute(player);
        }
    }

    public void deleteAttribute(Player p) {
        if(TyRank.getConfigManager().getConfigData().getUseAttribute().equals("ap")) {
            AttributeData attributeData = AttributeAPI.getAttrData(p);
            AttributeAPI.takeSourceAttribute(attributeData, "TyRank");
        }
    }
}
